package com.caiyi.tips.user.controller;

import com.caiyi.tips.core.VO.ResultVO;
import com.caiyi.tips.core.enums.ResultEnum;
import com.caiyi.tips.core.util.CheckUtil;
import com.caiyi.tips.core.util.StringUtil;
import com.caiyi.tips.core.util.TipsUtil;
import com.caiyi.tips.user.DTO.SmsDTO;
import com.caiyi.tips.user.DTO.UserRegisterDTO;
import com.caiyi.tips.user.bean.SmsBean;
import com.caiyi.tips.user.bean.UserBean;
import com.caiyi.tips.user.exception.UserException;
import com.caiyi.tips.user.service.SmsService;
import com.caiyi.tips.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/*
 * @author Cliff.ZhouYang
 * @date 2017/11/8 15:42
 */

@RequestMapping("/user")
@RestController
@Slf4j
@Validated
@Api(value = "普通用户接口", tags = "5.跟单普通用户", description = "登录，注册，创建用户等相关接口", protocols = "http")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private SmsService smsService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation(value = "注册接口", notes = "需要前置接口，发送短信验证码\n\n" +
            "密码字段加密原则: 加盐md5的32位小写")
    public ResultVO register(HttpServletRequest request, UserRegisterDTO dto) {
        if (dto.getUserType() == null) {
            throw new UserException("用户类型不正确");
        }
        if (StringUtils.isEmpty(dto.getPhone())) {
            throw new UserException("手机号不能为空");
        }
        if (!CheckUtil.isMobilephone(dto.getPhone())) {
            throw new UserException("手机号格式不正确");
        }
        if (StringUtils.isEmpty(dto.getSource())) {
            throw new UserException("渠道不正确");
        }
        if (StringUtils.isEmpty(dto.getPassword())) {
            throw new UserException("密码不能为空");
        }
        if (StringUtils.isEmpty(dto.getYzm())) {
            throw new UserException("验证码不能为空");
        }
        UserBean userBean = new UserBean();
        BeanUtils.copyProperties(dto, userBean);
        userBean.setIpAddr(TipsUtil.getRealIp(request));
        return userService.register(userBean);
    }

    @ApiOperation(value = "登录接口", notes = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cuserId", value = "手机号", required = true, dataType = "String", paramType =
                    "form"),
            @ApiImplicitParam(name = "cpassword", value = "密码", required = true, dataType = "String", paramType =
                    "form"),
            @ApiImplicitParam(name = "cloginfrom", value = "登录来源 来源 HSK:惠刷卡 GJJ:公积金 JZ:记账", required = true, dataType
                    = "String", paramType =
                    "form"),
            @ApiImplicitParam(name = "csource", value = "渠道值", required = true, dataType = "String", paramType =
                    "form"),
            @ApiImplicitParam(name = "ipAddr", value = "客户端的实际ip", required = true, dataType = "String", paramType =
                    "form"),
            @ApiImplicitParam(name = "mobileType", value = "客户端类型 1 安卓 2 iOS 3 其他", required = true, dataType =
                    "int", paramType =
                    "form"),
            @ApiImplicitParam(name = "packageName", value = "包名", required = false, dataType = "String", paramType =
                    "form")
    })
    @PostMapping(value = "/login")
    public ResultVO login(UserBean userBean) {
        if (StringUtils.isEmpty(userBean.getUserid())) {
            throw new UserException("账号不能为空");
        }
        if (StringUtils.isEmpty(userBean.getPassword())) {
            throw new UserException("密码不能为空");
        }
        if (userBean.getCloginfrom() == null) {
            throw new UserException("登录来源不能为空");
        }
        if (StringUtils.isEmpty(userBean.getIpAddr())) {
            throw new UserException("客户端ip不能为空");
        }
        return userService.login(userBean);
    }
}
